
package com.huaweicloud.saaslanding.install.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huaweicloud.saaslanding.basic.enumeration.MonoDeploymentInfoBelongEnum;
import com.huaweicloud.saaslanding.basic.utils.EncryptionUtil;
import com.huaweicloud.saaslanding.dao.common.entity.app.MonoAppDeployment;
import com.huaweicloud.saaslanding.dao.common.entity.cloudservice.IamAccountInfo;
import com.huaweicloud.saaslanding.dao.common.entity.security.WorkSecretKey;
import com.huaweicloud.saaslanding.dao.common.repository.app.MonoAppDeploymentBaseMapper;
import com.huaweicloud.saaslanding.dao.common.repository.cloudservice.EndpointInfoBaseMapper;
import com.huaweicloud.saaslanding.dao.common.repository.cloudservice.IamAccountInfoBaseMapper;
import com.huaweicloud.saaslanding.dao.common.repository.security.WorkSecretKeyBaseMapper;
import com.huaweicloud.saaslanding.huawei.iam.api.IamApi;
import com.huaweicloud.saaslanding.huawei.vpc.model.HeadersDto;

import lombok.extern.slf4j.Slf4j;

import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.util.Map;

import jakarta.annotation.Resource;

/**
 * 功能描述
 *
 * @since 2022-12-15
 */
@Slf4j
@Component
public class ApiHandleUtil {

    @Resource
    private Environment environment;

    @Resource
    private MonoAppDeploymentBaseMapper monoAppDeploymentBaseMapper;

    @Resource
    private IamAccountInfoBaseMapper iamAccountInfoMapper;

    @Resource
    private WorkSecretKeyBaseMapper workSecretKeyBaseMapper;

    @Resource
    private EndpointInfoBaseMapper endpointInfoMapper;

    /**
     * 构造带有 IAM Token 的headers
     *
     * @param appId 应用模板表 id
     * @return headers
     */
    public HeadersDto getIamHeader(Long appId) {
        // 单体应用部署信息
        MonoAppDeployment monoAppDeployment = monoAppDeploymentBaseMapper
                .selectOne(new QueryWrapper<MonoAppDeployment>().eq("app_id", appId).eq("belong_to", MonoDeploymentInfoBelongEnum.SERVER.getBelongTo()));

        // IAM 信息
        IamAccountInfo iamAccountInfo = iamAccountInfoMapper.selectById(monoAppDeployment.getIamId());

        String database = environment.getProperty("saaslanding.mysql.database");
        String saasSalt = getSalt(database);
        String password = EncryptionUtil.decrypt(saasSalt, iamAccountInfo.getPassword());

        HeadersDto headersDto = new HeadersDto();
        headersDto.setIamEndpoint(iamAccountInfo.getEndpoint());
        headersDto.setIamPassword(password);
        headersDto.setIamDomainName(iamAccountInfo.getDomainName());
        headersDto.setIamUserName(iamAccountInfo.getIamName());
        headersDto.setProjectId(monoAppDeployment.getProjectId());

        headersDto.setVpcEndpoint(endpointInfoMapper.getVpcEndpoint());

        return headersDto;
    }

    /**
     * 获取iam请求头信息
     *
     * @param appId 应用id
     * @return 请求头信息
     */
    public Map<String, String> getIamHeaderMap(Long appId) {
        HeadersDto headersDto = getIamHeader(appId);
        return IamApi.getIamHeader(headersDto.getIamEndpoint(), headersDto.getIamDomainName(),
                headersDto.getIamUserName(), headersDto.getIamPassword(), headersDto.getProjectId());
    }

    /**
     * 获取口令
     *
     * @param tenantCode 租户code
     * @return 口令
     */
    public String getSalt(String tenantCode) {
        WorkSecretKey workSecretKey = workSecretKeyBaseMapper.getSaltByTenantCode(tenantCode);
        return EncryptionUtil.decrypt(workSecretKey.getPwdSalt(), workSecretKey.getPassword());
    }

    /**
     * 获取项目ID
     *
     * @param appId 应用模板表 id
     * @return ProjectId
     */
    public String getProjectId(Long appId) {
        // 单体应用部署信息
        MonoAppDeployment monoAppDeployment = monoAppDeploymentBaseMapper
                .selectOne(new QueryWrapper<MonoAppDeployment>().eq("app_id", appId).eq("belong_to", MonoDeploymentInfoBelongEnum.SERVER.getBelongTo()));
        return monoAppDeployment.getProjectId();
    }

    /**
     * 获取 ECS 终端节点
     *
     * @param appId 应用模板表 id
     * @return String
     */
    public String getEcsEndpoint(Long appId) {
        // 单体应用部署信息
        MonoAppDeployment monoAppDeployment = monoAppDeploymentBaseMapper
                .selectOne(new QueryWrapper<MonoAppDeployment>().eq("app_id", appId).eq("belong_to", MonoDeploymentInfoBelongEnum.SERVER.getBelongTo()));
        return endpointInfoMapper.findEcsEndpoint(monoAppDeployment.getProjectId());
    }
}
